# @Time : 2020/6/28 15:31
# @Author : Fioman 
# @Phone : 13149920693
import copy

"""
浅拷贝没有什么问题,但是有时候我们需要的是深拷贝(即副本不光想内部对象的引用)
copy模块提供的deepcopy和copy函数能为任意对象做深拷贝和浅拷贝.
"""


class Bus:
    def __init__(self, passengers=None):
        if passengers is None:
            self.passengers = []
        else:
            self.passengers = list(passengers)

    def pick(self, name):
        self.passengers.append(name)

    def drop(self, name):
        self.passengers.remove(name)


bus1 = Bus(["Alice", "Bill", "Claire", "David"])
bus2 = copy.copy(bus1)
bus3 = copy.deepcopy(bus1)

print("id(bus1) = {},id(bus2) = {},id(bus3) = {}".format(id(bus1), id(bus2), id(bus3)))
bus1.drop("Bill")
print("bus2.passengers = {}".format(bus2.passengers))
print("bus3.passengers = {}".format(bus3.passengers))

print("id(bus1.passengers) = {},id(bus2.passengers) = {},id(bus3.passengers) = {}".format(id(bus1.passengers),id(bus2.passengers),id(bus3.passengers)))
"""
一般来说,深复制不是件简单的事.如果对象有循环引用,那么这个朴素的算法会进入无线循环.
deepcopy函数会记住已经复制的对象,因此能优雅地处理循环引用,如果8_10所示.
"""

a = [10,20]
b = [a,30]

a.append(b)
print("a = {}".format(a))
c = copy.deepcopy(a)
print("c = {}".format(c))


















